﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace XFFSM
{
    public class XFFSMMenu
    {
        [MenuItem("Assets/Create/XFFSM/FSMController")]
        static void CreateFSM() {

            RuntimeFSMControllerCreator runtimeFSMControllerCreator = ScriptableObject.CreateInstance<RuntimeFSMControllerCreator>();

            string fileName = GetName();

#if UNITY_2018 
            GUIContent content = EditorGUIUtility.IconContent("icons/processed/unityengine/billboardasset icon.asset");
#elif UNITY_2019_1_OR_NEWER
            GUIContent content = EditorGUIUtility.IconContent("d_ScriptableObject Icon");
#endif

            ProjectWindowUtil.StartNameEditingIfProjectWindowExists(runtimeFSMControllerCreator.GetInstanceID(), runtimeFSMControllerCreator, fileName, (Texture2D)content.image, null);
        }

        static string GetName(string templateName = "New FSMController", string suffix = "asset") {

            string name;
            string[] files;

            int i = 0;

            do
            {
                name = string.Format("{0}{1}", templateName, i == 0 ? string.Empty : i.ToString());
                files = AssetDatabase.FindAssets(name);

                i++;
            } while (files != null && files.Length != 0);

            return string.Format("{0}.{1}", name, suffix);
        }

        [MenuItem("Assets/Create/XFFSM/FSMState")]
        static void CreateFSMState() {
            FSMStateCreator stateCreator = ScriptableObject.CreateInstance<FSMStateCreator>();

            string fileName = GetName("NewState", "cs");

            GUIContent content = EditorGUIUtility.IconContent("icons/processed/cs script icon.asset");

            ProjectWindowUtil.StartNameEditingIfProjectWindowExists(stateCreator.GetInstanceID(), stateCreator, fileName, (Texture2D)content.image, "Assets/XFFSM/Editor/Template/FSMStateTemplate.txt");
        }

        [MenuItem("Assets/Create/XFFSM/About")]
        static void About()
        {
            //Debug.Log("About");
            Rect rect = new Rect(0, 0, 550, 350);
            FSMAboutWindow window = EditorWindow.GetWindowWithRect<FSMAboutWindow>(rect, true, "About XFFSM");
            window.Show();
        }

    }

}